package com.whpost.service.tools;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.HorizontalAlignment;

import java.util.*;
import java.io.File;

// 兑换码实体类
class RedeemCodeData {
    private String code;

    public RedeemCodeData() {}

    public RedeemCodeData(String code) {
        this.code = code;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }
}

public class RedeemCodeGenerator {

    // 生成指定数量的兑换码
    public static List<String> generateCodes(String prefix, int count) {
        Set<String> codeSet = new HashSet<>();
        Random random = new Random();
        String numbers = "0123456789";
        String letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

        // 确保生成不重复的兑换码
        while (codeSet.size() < count) {
            // 生成10位随机数字
            StringBuilder numPart = new StringBuilder();
            for (int i = 0; i < 10; i++) {
                numPart.append(numbers.charAt(random.nextInt(numbers.length())));
            }

            // 随机决定替换3位还是4位
            int replaceCount = random.nextBoolean() ? 3 : 4;
            // 生成不重复的替换位置
            Set<Integer> positions = new HashSet<>();
            while (positions.size() < replaceCount) {
                positions.add(random.nextInt(10)); // 0-9的位置
            }

            // 替换指定位置为字母
            char[] codeChars = numPart.toString().toCharArray();
            for (int pos : positions) {
                codeChars[pos] = letters.charAt(random.nextInt(letters.length()));
            }

            // 拼接前缀生成完整兑换码
            String fullCode = prefix + new String(codeChars);
            codeSet.add(fullCode);
        }

        return new ArrayList<>(codeSet);
    }

    // 导出到Excel
    public static void exportToExcel(List<String> codes, String filePath) {
        // 设置单元格样式
        WriteCellStyle contentStyle = new WriteCellStyle();
        contentStyle.setHorizontalAlignment(HorizontalAlignment.LEFT);
        HorizontalCellStyleStrategy styleStrategy = new HorizontalCellStyleStrategy(null, contentStyle);

        // 转换为实体类列表
        List<RedeemCodeData> dataList = new ArrayList<>();
        for (String code : codes) {
            dataList.add(new RedeemCodeData(code));
        }

        // 写入Excel
        EasyExcel.write(filePath, RedeemCodeData.class)
                .registerWriteHandler(styleStrategy)
                .sheet("兑换码列表")
                .doWrite(dataList);
    }

    public static void main(String[] args) {
        // 可根据需要修改前缀和数量
        String prefix = "DM";  // 前缀
        int count = 100;       // 数量
        String filePath = "D:\\dhm.xls";  // 保存路径

        try {
            // 生成兑换码
            List<String> codes = generateCodes(prefix, count);

            // 导出到Excel
            exportToExcel(codes, filePath);

            System.out.println("兑换码生成成功，共" + count + "个，已保存至：" + filePath);
        } catch (Exception e) {
            System.err.println("生成失败：" + e.getMessage());
            e.printStackTrace();
        }
    }
}